import type { SiteData } from '@/stores/modules/site/interface'
import type { LoginFormType } from '../api/interface'

import { makeAutoObservable } from 'mobx'
import { loginApi } from '../api'
import { stores } from '@/stores'
import { fetchSiteInfo } from '@/api/base'
import { setSiteTitle } from '@/stores/modules/site/siteInfo'

class Store {
    siteInfo = {} as SiteData
    captchaRequestId: string | undefined = undefined

    constructor() {
        makeAutoObservable(this)
        this.fetchSiteInfoToStore()
    }

    /** 获取站点信息 */
    fetchSiteInfoToStore = async () => {
        try {
            const { data } = await fetchSiteInfo()
            this.siteInfo = data
            setSiteTitle()
        } catch (error) {}
    }

    /** 更新滑块验证ID */
    updateRequestId = (id?: string) => {
        this.captchaRequestId = id
    }

    login = async (value: LoginFormType) => {
        const params: LoginFormType = { ...value, request_id: this.captchaRequestId }

        let data = await loginApi(params)
        if (!data) return false

        stores.userStore.handleLoginData(data)
    }
}

export default Store
